package com.my.study.web.config;

import com.my.study.web.Interceptor.WebSocketInterceptor;
import com.my.study.web.handler.SocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;

@Configuration
public class WebSocketConfig implements WebSocketConfigurer {

    @Bean
    public WebSocketHandler createWebSocketHandler(){
        return new SocketHandler();
    }

    @Bean
    public HandshakeInterceptor createHandshakeInterceptor(){
        return new WebSocketInterceptor();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(createWebSocketHandler())
                .addInterceptors(createHandshakeInterceptor())
                .setAllowedOrigins("*")
                .withSockJS();// 指定协议
    }
}

